import Vue from 'vue'
import axios from 'axios'

// 初始化axios的通用配置
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API
})
// request请求统一拦截器
service.interceptors.request.use(config => {
  const token = Vue.$cookies.get('token')
  if (token) {
    config.headers.token = token
  }
  config.headers.contentType = 'application/x-www-form-urlencoded;charset=utf-8'
  return config
})

const http = {
  get (url, params) {
    return service.get(url, { params })
  },
  post (url, params) {
    return service.post(url, params)
  },
  delete (url, params) {
    return service.delete(url, { params })
  }
}

Vue.prototype.$axios = service
Vue.prototype.$http = http
Vue.prototype.$ajax = axios
